#include "writeWorld.hpp"
#include "Log.hpp"
#include <algorithm>
using namespace std;
using namespace m3g;

/**
*
*/
unsigned int writeWorld (M3GWriter* writer, unsigned int camera_index, unsigned int background_index, std::vector<unsigned int>& child_indices)
{
	Log:: print ("Write World : ");
	Log:: print ("   active camera = %d"  , camera_index    );
	Log:: print (",  background    = %d\n", background_index);

	int child_count = child_indices.size();

	M3GObject3DStruct      obj;
	M3GTransformableStruct tra;
	M3GNodeStruct          nod;
	M3GGroupStruct         grp;
	M3GWorldStruct         wld;
	grp.children_index_count = child_count;
	grp.children_index       = new unsigned int[child_count];
	for (int i = 0; i < child_count; i++) {
		grp.children_index[i]    = child_indices[i];
	}

	unsigned int object_index;

	wld.active_camera_index  = camera_index;
	wld.background_index     = background_index;
	writer->startObject        (M3G_TYPE_WORLD, &object_index);
	writer->writeObject3D      (obj);
	writer->writeTransformable (tra);
	writer->writeNode          (nod);
	writer->writeGroup         (grp);
	writer->writeWorld         (wld);
	writer->endObject          ();

	return object_index;
}


